<?php
	class ReplyBuilder
	{
		public static function get($error_code, $request, $error_msg_args = array())
		{
			self::init();

			$reply = array();

			$lang = NULL;
			if (array_key_exists('lang', $request))
				$lang = (string)$request['lang'];

			// FIXME: Potential DOS issue here. 
			// Jiaoxiaodong, 2013/7/7
			if (array_key_exists('extra', $request))
				$reply['extra'] = $request['extra'];

			$reply['error'] = $error_code;
			if ($lang != NULL &&
				array_key_exists($error_code, self::$errors) &&
				array_key_exists($lang, self::$errors[$error_code]))
			{
				$reply['error_message'] = self::$errors[$error_code][$lang];
				if (count($error_msg_args) > 0)
				{
					array_unshift($error_msg_args, $reply['error_message']);
					$reply['error_message'] = 
						call_user_func_array('sprintf', $error_msg_args);
				}
			}

			return $reply;
		}

		private static function init()
		{
			static $init = false;
			if (!$init)
			{
				$init = true;
				require_once('config/error.cnf');
			}
		}

		private static $errors = NULL;
	}

?>